package com.internetCafes.spms.web.sys.controller;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.common.utils.entity.RR;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import com.internetCafes.spms.core.utils.Query;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.sys.entity.dto.profession.CertificateProfessionalSaveDTO;
import com.internetCafes.spms.web.sys.entity.dto.profession.CertificateProfessionalUpdateDTO;
import com.internetCafes.spms.web.sys.entity.vo.profession.ProfessionalsOrCategoriesVO;
import com.internetCafes.spms.web.sys.model.UserInfo;
import com.internetCafes.spms.web.sys.service.CertificateProfessionalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 证书专业
 *
 * @author Zj
 */
@Api(tags = {">>>证书专业"})
@RequestMapping("/sys/certificate/professional")
@RestController
public class CertificateProfessionalController {

    @Autowired
    private CertificateProfessionalService certificateProfessionalService;

    /**
     * 证书专业删除
     */
    @ApiOperation(value = "证书专业删除")
    @GetMapping("/delete/{id}")
    @ApiParam(required = true, name = "code", value = "证书专业id")
    public RR delete(@PathVariable("id") Long confId) {
        certificateProfessionalService.delete(confId);
        return new RR(0, "删除成功");
    }

    /**
     * 证书专业修改
     */
    @ApiOperation(value = "证书专业修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:certificateProfessional:update")
    public RR update(@RequestBody CertificateProfessionalUpdateDTO dto) {
        certificateProfessionalService.update(dto);
        return new RR(0, "修改成功");
    }

    /**
     * 获取类别列表
     */
    @ApiOperation(value = "获取类别列表")
    @GetMapping("/categories/{code}")
    @ApiParam(required = true, name = "code", value = "分类编码")
    @RequiresPermissions("sys:certificateProfessional:list")
    public RR<List<ProfessionalsOrCategoriesVO>> categories(@PathVariable("code") String code) {
        List<ProfessionalsOrCategoriesVO> categories = certificateProfessionalService.categories(code);
        return new RR<>(0, "请求成功", categories);
    }

    /**
     * 获取类型列表
     */
    @ApiOperation(value = "获取类型列表")
    @GetMapping("/types")
    @RequiresPermissions("sys:certificateProfessional:list")
    public RR<List<ProfessionalsOrCategoriesVO>> types() {
        List<ProfessionalsOrCategoriesVO> types = certificateProfessionalService.types();
        return new RR<>(0, "请求成功", types);
    }

    /**
     * 新增专业接口
     */
    @ApiOperation(value = "证书专业新增")
    @PostMapping("/save")
    @RequiresPermissions("sys:certificateProfessional:save")
    public RR save(@RequestBody CertificateProfessionalSaveDTO dto) {
        if (dto.getSaveTag() == null || StringUtils.isBlank(dto.getType())
                || StringUtils.isBlank(dto.getName()) || dto.getStatus() == null) {
            return new RR(500, "参数错误");
        }
        certificateProfessionalService.save(dto);
        return new RR(0, "保存成功");
    }

    /**
     * 证书专业列表
     *
     * @author Zj
     */
    @ApiOperation(value = "证书专业列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:certificateProfessional:list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request) {

        //查询列表数据
        Query query = new Query(params);
        params = new HashMap<>();

        String typeOrProfessional = request.getParameter("typeOrProfessional");

        if (StringUtils.isNotBlank(typeOrProfessional)) {
            params.put("typeOrProfessional", typeOrProfessional);
        }
        params.put("moduleTag", CommonConstants.CONF_Certificate_Type);

        DataGrid dataGrid = new DataGrid();
        UserInfo u = ShiroUtils.getUserEntity();
        if (u.getType() == CommonConstants.UserInfoType.SYSTEM_USER) {
            dataGrid = certificateProfessionalService.dataGrid(params, query.getPage(), query.getLimit());
        }

        return R.ok().put("page", dataGrid);
    }
}
